/**
 * undefined
 * @date    2020-03-13 02:38:56
 * @Author  likai
 * @email   
 */
import React,{useState} from 'react'
import { Form, Input, Button,Spin } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons'
import md5 from 'md5'
import utils from '@utils'
import { genPath } from "@contants/app";
import {setUserInfo,setResourceMenu,setResourceButtons,} from '@utils/store'
import {getAddress} from '@utils/address'
import css from './index.scss'
const NormalLoginForm = props => {
	const [form] = Form.useForm()
	// let [spinning,setSpinning] = useState(false)
  
  
  form.setFieldsValue({
    userName: "admin",
    password: "sucai@admin",
  });


  const onFinish = values => {
  	// setSpinning(true)
  	utils.post(genPath('/api/login'),{
  		userName:values.userName,
  		password:md5(values.password),
  		showMsg:false
  	}).then(res=>{
	  	if(res.errorCode==0){
		  	getUserInfo()
	  	}else{
        // setSpinning(false)
      }
  	}).catch(error=>{
	  	// setSpinning(false)
  		console.log(error)
  	})
  }

  const getUserInfo = ()=>{
  	// getAddress()
  	utils.get(genPath('/api/user/getCurrentUser'),{showMsg:false})
  	.then(res=>{
  		// if(res.errorCode==0){
      //   let {buttonList} = res.result
		  // 	setUserInfo(res.result)
		  // 	// setSpinning(false)
      //   setResourceButtons(buttonList)
  		// 	let {menuList} = res.result
	  	// 	let tree = utils.getTreeChild(menuList,1)
	  	// 	// 获取权限菜单的第一个路径显示，如果写死一个路径有可能出现空白页
	  	// 	let children = tree.reduce((arr,item)=>{
	  	// 		let child = item.children ||[]
	  	// 		arr.push(...child)
	  	// 		return arr
	  	// 	},[])
	  	// 	if(children.length){
	  	// 		let {url} = children[0] 
		  // 		window.location.href=url
	  	// 	}
  		// }
      window.location.href="/order/deal"
  	})
  }

  return (
    <div className={css.main}>
      <div className={css.image}></div>
      <Form className={css.loginForm} form={form} onFinish={onFinish} >
        <div className={css.title}>财务管理平台</div>
        {/* <div className={css.small}>智能平台</div> */}
        <Form.Item
          name="userName"
          rules={[{ required: true, message: '请输入用户名称' }]}
        >
          <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="请输入用户名称" />
        </Form.Item>
        
        <Form.Item
          name="password"
          rules={[{ required: true, message: '请输入密码' }]}
        >
          <Input
            prefix={<LockOutlined className="site-form-item-icon" />}
            type='password'
            placeholder="请输入密码"
          />

        </Form.Item>
        
        <Form.Item>
          <Button type="primary" htmlType="submit" className={css.loginFormButton}>
            登录
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
}
// const App = Form.create()(<NormalLoginForm />)
export default props=>{
	
	return <div className={css.bg}>
		<NormalLoginForm></NormalLoginForm>
	</div>
}
